package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Reservation;
import com.zzyl.entity.SurplusTime;
import com.zzyl.mapper.CustomerReservationMapper;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.SurplusTimeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Service
public class CustomerReservationServiceImpl implements CustomerReservationService {
    @Autowired
    private CustomerReservationMapper customerReservationMapper;
    @Override
    public Integer getCancelledReservationCount(Long userId) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime beginOfDay = LocalDateTimeUtil.beginOfDay(now);
        LocalDateTime endOfDay = LocalDateTimeUtil.endOfDay(now);
        return customerReservationMapper.getByUserId(userId,beginOfDay,endOfDay);
    }

    @Override
    public List<SurplusTimeVo> getReservationCountByTime(Long time) {
        LocalDateTime localDateTime = LocalDateTimeUtil.of(time);
        LocalDate now = localDateTime.toLocalDate();
        LocalDateTime startOfDay = now.atStartOfDay();
        LocalDateTime endOfDay = now.atTime(23, 59, 59, 999999999);

        List<SurplusTime> surplusTimes = customerReservationMapper.getByUserIdTime(startOfDay, endOfDay);
        List<SurplusTimeVo> surplusTimeVos= new ArrayList<>();
        for (SurplusTime surplusTime : surplusTimes) {
            SurplusTimeVo surplusTimeVo = new SurplusTimeVo();
            surplusTimeVo.setTime(surplusTime.getTime());
            surplusTimeVo.setCount(surplusTime.getCount());
            surplusTimeVos.add(surplusTimeVo);
        }

        return surplusTimeVos;

    }

    @Override
    public void add(ReservationDto reservationDto) {
        Long userId = UserThreadLocal.getUserId();

        if ( getCancelledReservationCount(userId)>3) {
            throw new RuntimeException("预约失败，您今天已经预约过了");
        }
        Reservation reservation = new Reservation();
        BeanUtils.copyProperties(reservationDto, reservation);
        reservation.setStatus(0);
        customerReservationMapper.add(reservation);
    }

    @Override
    public PageResponse<ReservationVo> page(Integer pageNum, Integer pageSize,Long userId,Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Reservation> page = customerReservationMapper.page(userId,status);
        return PageResponse.of(page, ReservationVo.class);
    }

    @Override
    public void cancel(Long id, Long userId) {
        customerReservationMapper.cancel(id,userId);
    }

    @Scheduled(cron = "0 1,31 * * * ?")
    public void timedTask(){

        customerReservationMapper.timedTask();

    }
}
